﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Dynamic;
using System.Web;

namespace Proggy.Web.Cms.Extensions.Html
{
    /// <summary>
    /// Back end content extensions
    /// </summary>
    public static class ProggyBackEndContentExtensions
    {
        /// <summary>
        /// Add to content which will be rendered in the layout
        /// </summary>
        /// <param name="html"></param>
        /// <param name="pathToContent">eg "~/Areas/MyArea/Content/Css/mycss.css"</param>
        public static void AddToBackEndContent(this HtmlHelper html, string pathToContent, BackEndContentType type)
        {
            if (HttpContext.Current != null && HttpContext.Current.Items != null)
            {
                // add if required
                if (HttpContext.Current.Items["temp_viewbag_rj"] == null)
                {
                    // add viewbag 
                    var viewbag_ = new BackEndContentStore();
                    HttpContext.Current.Items["temp_viewbag_rj"] = viewbag_;
                    viewbag_.BackEndContent = new List<Tuple<string, BackEndContentType>>();
                }

                // get view bag
                var viewbag = (BackEndContentStore)HttpContext.Current.Items["temp_viewbag_rj"];  //  html.ViewContext.Controller.ViewBag;

                // get 
                var content = viewbag.BackEndContent;

                // check already in
                if (!content.Select(x => x.Item1.ToLower()).Contains(pathToContent.ToLower()))
                {
                    content.Add(new Tuple<string, BackEndContentType>(pathToContent.ToLower(), type));
                }
            }

        }

        /// <summary>
        /// Renders content added to back-end
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static MvcHtmlString RenderBackEndContent(this HtmlHelper html)
        {
            try
            {
                if (HttpContext.Current.Items["temp_viewbag_rj"] != null || ((BackEndContentStore)HttpContext.Current.Items["temp_viewbag_rj"]).BackEndContent != null)
                {

                    // get view bag
                    var viewbag = (BackEndContentStore)HttpContext.Current.Items["temp_viewbag_rj"];

                    // check
                    if (viewbag.BackEndContent == null || viewbag.BackEndContent.GetType() != typeof(List<Tuple<string, BackEndContentType>>))
                    {
                        viewbag.BackEndContent = new List<Tuple<string, BackEndContentType>>();
                    }

                    // get content
                    var content = (List<Tuple<string, BackEndContentType>>)viewbag.BackEndContent;

                    // start string
                    var output = new StringBuilder();

                    // iterate
                    foreach (var c in content)
                    {

                        // get path
                        var path = UrlHelper.GenerateContentUrl(c.Item1, html.ViewContext.HttpContext);

                        // type
                        switch (c.Item2)
                        {
                            case BackEndContentType.Stylesheet:
                                {
                                    output.Append("\t<link rel=\"stylesheet\" href=\"");
                                    output.Append(path);
                                    output.Append("\" />\n");
                                    break;
                                }
                            case BackEndContentType.JavaScript:
                                {
                                    output.Append("\t<script type=\"text/javascript\" src=\"");
                                    output.Append(path);
                                    output.Append("\"></script>\n");
                                    break;
                                }
                        }

                    }

                    //
                    return MvcHtmlString.Create(output.ToString());
                }
            }
            catch
            {
            }

            //
            return MvcHtmlString.Create("");

        }

        /// <summary>
        /// For back end partials which will be rendered in lightbox
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static void RenderBackEndContentForLightboxPartial(this HtmlHelper html)
        {
            try
            {
                if (HttpContext.Current.Items["temp_viewbag_rj"] != null || ((BackEndContentStore)HttpContext.Current.Items["temp_viewbag_rj"]).BackEndContent != null)
                {

                    // start output
                    var output = new StringBuilder();

                    // go
                    output.Append("<script type=\"text/javascript\">\n");

                    // add array
                    output.Append("\twindow.LIGHTBOX_CONTENT = [];\n");

                    // add registration cache
                    //output.Append("\twindow.LIGHTBOX_CONTENT_REGISTRATION = [];\n");

                    // get view bag
                    var viewbag = (BackEndContentStore)HttpContext.Current.Items["temp_viewbag_rj"];  //  html.ViewContext.Controller.ViewBag;

                    // get content
                    var content = (List<Tuple<string, BackEndContentType>>)viewbag.BackEndContent;

                    // iterate
                    foreach (var c in content)
                    {
                        // get path
                        var path = UrlHelper.GenerateContentUrl(c.Item1, html.ViewContext.HttpContext);

                        // add to output
                        output.Append("\twindow.LIGHTBOX_CONTENT.push({ type:'");
                        output.Append(c.Item2);
                        output.Append("', path: '");
                        output.Append(path);
                        output.Append("'});\n");

                        // add registration
                        //output.Append("\twindow.LIGHTBOX_CONTENT_REGISTRATION.push(\"");
                        //output.Append(path);
                        //output.Append("\");");
                    }

                    // end
                    //output.Append("\tconsole.log(window.LIGHTBOX_CONTENT);\n");
                    output.Append("</script>");

                    // write
                    html.ViewContext.Writer.Write(output.ToString());
                }
            }
            catch { 
                
            }
        }
    }
}
